/**
 * 
 */
package org.ccay.excel.importer.service.impl.internal;

import java.util.List;
import java.util.Map;

/**
 * VO转换器的抽象实现。
 * 
 * @author chaowangbang
 *
 */
public abstract class AbstractVOConverter implements IVOConverter {
	
	/** VO对应的Class **/
	protected Class<?> voClass;
	
	
	public AbstractVOConverter() {
	}
	
	/**
	 * @param voClass
	 */
	public AbstractVOConverter(Class<?> voClass) {
		this.voClass = voClass;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.service.impl.internal.IVOConverter#convert(java.util.Map)
	 */
	public final Object convert(Map<String, Object> voProperties) throws ConvertException {
		try {
			Object instance = voClass.newInstance();
			populateObject(instance, voProperties);
			return instance;
		} catch (Exception ex) {
			throw new ConvertException(ex);
		}
	}
	/**
	 * 将对象的属性设置到map中
	 * @param voProperties
	 * @return
	 * @throws ConvertException
	 */
	public final List<Object>  reflect(Object instance,String[] voProperties)throws ConvertException{
		try {
			if(null == instance || null == voProperties || voProperties.length == 0){
				return null;
			}
			return getProperties(instance, voProperties);
		} catch (Exception ex) {
			throw new ConvertException(ex);
		}
	}
	/**
	 * 根据对象的属性值组装一个对象实例。
	 * 
	 * @param instance
	 * @param voProperties
	 * @throws Exception
	 */
	protected abstract void populateObject(Object instance, Map<String, Object> voProperties)  throws ConvertException;
	
	/**
	 * 根据对象的属性返回一个map。
	 * 
	 * @param instance
	 * @param voProperties
	 * @throws Exception
	 */
	protected abstract List<Object> getProperties(Object instance, String[] voProperties)  throws ConvertException;

}
